package com.rytong.tools.crypto;

import java.math.BigInteger;
import java.util.Random;

import com.rytong.tools.utils.Utils;

public class hsmcli // extends Applet
{
    private static String KEY;
    private static String EXPONENT;
    private static int PinMaxLen = 128;

    private static String genRandom(int i) throws Exception {
        byte abyte0[] = new byte[i];
        Random ran = new Random();
        for (int j = 0; j < i; j++) {
            int temp = (ran.nextInt() * 57) + 65;
            temp = Math.abs(temp) % 128;
            abyte0[j] = (byte) temp;
        }
        return new String(abyte0);
    }

    private static BigInteger mod(BigInteger biginteger, BigInteger biginteger1, BigInteger biginteger2) {
        BigInteger biginteger3 = null;
        try {
            biginteger3 = biginteger.modPow(biginteger1, biginteger2);
        } catch (ArithmeticException _ex) {
            return null;
        }
        return biginteger3;
    }

    public static String PkEncryptAPin(String s) throws Exception {
        if (null == KEY) {
            KEY = Utils.getConfigStringFormAsset(Utils.getActivity(), "hsmclikey");
        }
        if (null == EXPONENT) {
            EXPONENT = Utils.getConfigStringFormAsset(Utils.getActivity(), "hsmcliEXPONENT");
        }
        checkPassword(s);
        int i = s.length();
        if (i >= PinMaxLen - 2)
            return null;
        Integer integer = new Integer(i / 10 + 48);
        Integer integer1 = new Integer(i % 10 + 48);
        byte abyte0[] = { integer.byteValue(), integer1.byteValue() };
        String s2 = new String(abyte0) + s + genRandom(PinMaxLen - i - 2);
        byte[] pin_b = s2.getBytes();
        BigInteger in = new BigInteger(pin_b);
        BigInteger biginteger = new BigInteger(KEY, 16);
        BigInteger e = new BigInteger(EXPONENT);
        String s3 = mod(in, e, biginteger).toString(16);
        int j = s3.length();
        if (j < 256) {
            int k = 256 - j;
            String s4 = "";
            for (int l = 0; l < k; l++)
                s4 = s4 + "0";

            return s4 + s3;
        } else {
            return s3;
        }
    }

    public static String PkEncryptEPin(String s) throws Exception {
        if (null == KEY) {
            KEY = Utils.getConfigStringFormAsset(Utils.getActivity(), "hsmclikey");
        }
        if (null == EXPONENT) {
            EXPONENT = Utils.getConfigStringFormAsset(Utils.getActivity(), "hsmcliEXPONENT");
        }
        checkPassword(s);
        int i = s.length();
        if (i >= PinMaxLen - 2)
            return null;
        Integer integer = new Integer(0);
        Integer integer1 = new Integer(i);
        byte abyte0[] = { integer.byteValue(), integer1.byteValue() };
        String s2 = new String(abyte0) + s + genRandom(PinMaxLen - i - 2);
        byte[] pin_b = s2.getBytes();
        BigInteger in = new BigInteger(pin_b);
        BigInteger biginteger = new BigInteger(KEY, 16);
        BigInteger e = new BigInteger(EXPONENT);
        String s3 = mod(in, e, biginteger).toString(16);
        int j = s3.length();
        if (j < 256) {
            int k = 256 - j;
            String s4 = "";
            for (int l = 0; l < k; l++)
                s4 = s4 + "0";

            return s4 + s3;
        } else {
            return s3;
        }
    }

    private static void checkPassword(String s) throws Exception {
        if (s != null && s.length() >= 8) {
            int number = 0;
            int len = s.length() - 1;
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) == s.charAt(i + 1)) {
                    number++;
                } else {
                    number = 0;
                }
                // if (number >= 5) {// 出现连续6个相等的字符时报错误。
                // throw new Exception("密码太过简单，请重新输入。");
                // }
            }
        }
    }

}
